from quant_researcher.quant.datasource_fetch.crypto_api.binance import fetch_binance_exchangeinfo, fetch_binance_funding_rate
from quant_researcher.quant.project_tool.time_tool import get_today
import pandas as pd
from dateutil.tz import tzutc
import datetime
from utils import df_into_db, read_sql
import os
import time
from joblib import Parallel, delayed


DATA_DIR = f'E:\\指标数据'  # 用于存放测试数据
symbol_list = "all"
file_path = os.path.join(DATA_DIR, r'funding_rate\all_market_funding_rate')
os.makedirs(file_path, exist_ok=True)

end_date = get_today(marker='with_n_dash')
# timezone = 'Asia/Shanghai'
timezone = '+0000'
exist_df = read_sql("select distinct symbol from all_market_funding_rate", db_name="funding_rate")
if symbol_list == 'all':
    data_cm = fetch_binance_exchangeinfo(type='cm_futures', trading=False)
    data_cm = data_cm[data_cm['contractType'] == 'PERPETUAL']
    cm_symbol_list = list(data_cm['symbol'])
    data_um = fetch_binance_exchangeinfo(type='um_futures', trading=False)
    data_um = data_um[data_um['contractType'] == 'PERPETUAL']
    um_symbol_list = list(data_um['symbol'])

    symbol_list = []
    symbol_list.extend(cm_symbol_list)
    symbol_list.extend(um_symbol_list)


def inner_func(symbol):
    if '_PERP' in symbol:
        market_type = 'cm_futures'
        file_name = os.path.join(file_path, f'{symbol}_funding_rate')
    else:
        market_type = 'um_futures'
        file_name = os.path.join(file_path, f'{symbol}_funding_rate')

    if os.path.exists(f'{file_name}.xlsx'):
        history_funding_rate = pd.read_excel(f'{file_name}.xlsx', index_col='datetime')
        start_date = history_funding_rate.index[-1]
        if start_date[:10] == end_date:
            print(f'{symbol}资金费率数据已经最新')
            all_data = history_funding_rate['funding_rate']
            all_data.name = symbol
            if market_type == 'cm_futures':
                cm_data_df = all_data
            else:
                cm_data_df = None
            if market_type == 'um_futures':
                um_data_df = all_data
            else:
                um_data_df = None
            return all_data, cm_data_df, um_data_df
    else:
        start_date = '2019-01-01 00:00:00'
        history_funding_rate = pd.DataFrame()

    start = time.mktime(datetime.datetime(int(start_date[:4]), int(start_date[5:7]), int(start_date[8:10]), 0, 0, 0,
                                          tzinfo=tzutc()).timetuple())
    end = time.mktime(datetime.datetime(int(end_date[:4]), int(end_date[5:7]), int(end_date[-2:]), 23, 59, 59,
                                        tzinfo=tzutc()).timetuple())

    print(f'开始获取{symbol}资金费率数据')
    data = fetch_binance_funding_rate(symbol, market_type, start, end, '8h')
    if data is not None:

        data['timestamp'] = data.apply(lambda x: int(x['timestamp'] / 1000), axis=1)
        data['date'] = pd.to_datetime(data['timestamp'], unit='s').dt.tz_localize('UTC').dt.tz_convert(timezone)
        data['date'] = data['date'].dt.strftime('%Y-%m-%d')
        data['datetime'] = pd.to_datetime(data['timestamp'], unit='s').dt.tz_localize('UTC').dt.tz_convert(timezone)
        data['datetime'] = data['datetime'].dt.strftime('%Y-%m-%d %H:%M:%S')
        data['time'] = pd.to_datetime(data['timestamp'], unit='s').dt.tz_localize('UTC').dt.tz_convert(timezone)
        data['time'] = data['time'].dt.strftime('%H:%M:%S')
        data.set_index('datetime', inplace=True)
        data.sort_index(inplace=True)
        all_data = pd.concat([history_funding_rate, data])
    else:
        all_data = history_funding_rate
    all_data = all_data[~all_data.index.duplicated(keep='first')]
    if market_type == "cm_futures":
        all_data["type"] = "cm"
    elif market_type == "um_futures":
        all_data["type"] = "um"
    else:
        raise Exception("invalid market type")
    all_data.drop(columns=['date', 'time'], inplace=True)
    all_data = all_data.reset_index(drop=False)
    df_into_db(all_data, db_name='funding_rate', table_name='all_market_funding_rate')
    # file_name = os.path.join(file_path, f'{symbol}_funding_rate')
    # all_data.to_excel(f'{file_name}.xlsx')

    # all_data = all_data['funding_rate']
    # all_data.name = symbol
    # if market_type == 'cm_futures':
    #     cm_data_df = all_data
    # else:
    #     cm_data_df = None
    # if market_type == 'um_futures':
    #     um_data_df = all_data
    # else:
    #     um_data_df = None
    # time.sleep(1)
    #
    # return all_data, cm_data_df, um_data_df


for symbol in symbol_list:
    if symbol not in set(exist_df["symbol"]):
        inner_func(symbol)
        # time.sleep(5)
    else:
        print(f"{symbol} in db")





